//package com.studerb.tests.service;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import javax.swing.JOptionPane;
//
//import org.apache.commons.net.nntp.NNTPCommand;
//import org.apache.commons.net.nntp.NNTPReply;
//import org.apache.log4j.Logger;
//import org.junit.BeforeClass;
//import org.junit.Test;
//import static org.junit.Assert.*;
//
//import com.studerb.exceptions.ServiceException;
//import com.studerb.model.Server;
//import com.studerb.nntp.NntpConnection;
//import com.studerb.service.NntpConnectionPoolService;
//import com.studerb.service.ServerService;
//
//
//public class TestConnectionPoolService {
//	private static Logger log;
//	protected INntpConnectionPoolService connectionPoolService;
//	protected ServerService serverService;
//	
//	
//	@BeforeClass
//	public static void printMessage(){
//		log = Logger.getLogger(TestConnectionPoolService.class);
//		JOptionPane.showMessageDialog(null, "Make sure both good and bad server are inserted into db and good server has 9 connections");
//	}
//	
//	@Test
//	public void testPoolStatus(){
//		int active = connectionPoolService.getNumActive();
//		int idle = connectionPoolService.getNumIdle();
//		assertTrue(active == 0);
//		assertTrue(idle == 9);
//	}
//	
//	@Test
//	public void testBorrowObject() throws Exception {
//		Server server = serverService.getMasterServer();
//		NntpConnection connection = (NntpConnection) connectionPoolService.borrowObject(server);
//		assertNotNull(connection);
//		assertTrue(connection.isConnected());
//		
//		int active = connectionPoolService.getNumActive();
//		int idle = connectionPoolService.getNumIdle();
//		assertTrue(active == 1);
//		assertTrue(idle == 8);
//		connectionPoolService.returnObject(null, connection);
//		active = connectionPoolService.getNumActive();
//		idle = connectionPoolService.getNumIdle();
//		assertTrue(active == 0);
//		assertTrue(idle == 9);
//	}
//	
//	@Test
//	public void testBorrowTooMany() throws Exception {
//		NntpConnection conn;
//		List<NntpConnection> connections = new ArrayList<NntpConnection>();
//		Server server = serverService.getMasterServer();
//		int idle = connectionPoolService.getNumIdle();
//		//get try to get one more than is idle
//		for(int i = 0; i < idle; ++i){
//			log.debug("borrowed connection number: " + i);
//			conn = (NntpConnection) connectionPoolService.borrowObject(server);
//			connections.add(conn);
//			assertNotNull(conn);
//			assertTrue(conn.isConnected());
//		}
//		try {
//			log.debug("Borrowing object that should throw exception");
//			connectionPoolService.borrowObject(server);
//			fail("Should have thrown serviceException");
//		}
//		catch(ServiceException e){}
//		for(NntpConnection c: connections)
//			connectionPoolService.returnObject(null, c);
//	}
//	
//	@Test
//	public void testBorrowManyConnections() throws Exception {
//		Server server = serverService.getMasterServer();
//		final int TEST_RUNS = 50;
//		int initialIdle = connectionPoolService.getNumIdle(server);
//		assertTrue(initialIdle == server.getConnections());
//		int initialActive = connectionPoolService.getNumActive(server);
//		for (int i = 0; i < TEST_RUNS; ++i){
//			log.debug("Test: " + i);
//			NntpConnection conn = (NntpConnection) connectionPoolService.borrowObject(server);
//			assertEquals(connectionPoolService.getNumActive(server) - 1, initialActive);
//			assertEquals(connectionPoolService.getNumIdle(server) + 1, initialIdle);
//			int reply = conn.sendCommand(NNTPCommand.DATE);
//			if (reply != NNTPReply.DATE_REPLY)
//				throw new Exception("Error sending date command: " + conn.getReplyString());
//			connectionPoolService.returnObject(null, conn);
//			assertEquals(connectionPoolService.getNumActive(server), initialActive);
//			assertEquals(connectionPoolService.getNumIdle(server), initialIdle);
//		}
//		
//	}
//}
